<?php

namespace app\admin\server\vip;

use app\admin\common;
use app\admin\model\music\MusicSelectedPackModel;
use app\admin\model\user\UserModel;
use app\admin\model\vip\VipModel;
use app\admin\server\user\UserServer;
use app\admin\ServerBase;
use app\admin\validate\vip\VipValidate;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;

class VipServer extends ServerBase
{
    protected $model;
    protected $request;
    /**
     * @var mixed
     */
    protected $param;
    /**
     * @var mixed
     */
    protected $validate;

    function __construct( Request $request)
    {
        //$this->checkAuth();
        $this->request=$request;
        $this->param=$request->param();
        $this->model=new VipModel();
        $this->validate = new VipValidate();
    }

    function getList(){
        [$page,$limit] = $this->getPage($this->param);
        $list = $this->model->page($page,$limit)
            ->order('sort desc,id desc')
            ->select();
        $result['data']=$list;
        $result['count']=$this->model->count();
        $this->getMessageCode('adminMessage.success.select',2);
        return $result;
    }
    function getDetail(){
        $info = $this->model->getFindById($this->request->param('id'));
        $info['selected_pack_list'] = (new MusicSelectedPackModel())
            ->whereIn('id',$info['selected_pack'])
            ->select();
        $this->getMessageCode('adminMessage.success.select',2);
        return $info;
    }
    function add(){
        $post = $this->request->post();
        $post['selected_pack'] = implode(',',$post['selected_pack'])??[];
        if(!$this->validate->scene('create')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        if($this->model->save($post)){
            $this->getMessageCode('adminMessage.success.insert',2);
            return true;
        }else{
            $this->getMessageCode('adminMessage.error.insert',1);
            return false;
        }
    }
    function edit(){
        $id = $this->request->param('id',0);
        $post = $this->request->post();
        $post['id'] = $id;
        $post['selected_pack'] = implode(',',$post['selected_pack'])??[];
        if(!$this->validate->scene('edit')->check($post)){
            $this->ERROR = $this->validate->getError();
            return false;
        }
        $info  = $this->model->getFindById($id);
        if(empty($info)){
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
        if($info->save($post)){
            $this->getMessageCode('adminMessage.success.update',2);
            return true;
        }else{
            $this->getMessageCode('adminMessage.error.update',1);
            return false;
        }
    }

    function import_user(){

        if(!request()->file('file')){
            $this->getMessageCode('adminMessage.excel.import.fileTrue',1);
            return false;
        }
        try{
            $res=common::getImportDataFile('public','/');
            $excel_data=common::importData($res['path'],false,1);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息\
            $this->getMessageCode('adminMessage.excel.import.get_data',1);
            $this->ERROR=$this->error.','.$e->getError();
            return false;
        }
        Db::startTrans();
        try {
            $return =[];
            foreach ($excel_data as $key=>$val){
                $return[$key] =[
                    'code'=>1,
                    'msg'=>'成功'
                ];
                $mobile = $val['A']??'';
                if(empty($mobile)){
                    $return[$key]['code']=-1;
                    $return[$key]['msg']='用户手机号为空';
                    continue;
                }
                $user =UserModel::getInfoByobile($mobile);
                if(empty($user)){
                    $return[$key]['code']=-1;
                    $return[$key]['msg']='用户不存在';
                    continue;
                }
                if(empty($val['B'])){
                    $return[$key]['code']=-1;
                    $return[$key]['msg']='VIP为空';
                    continue;
                }
                $vip = $this->model->where('title',$val['B'])->find();
                if(empty($vip)){
                    $return[$key]['code']=-1;
                    $return[$key]['msg']='VIP不存在';
                    continue;
                }

                $user->vip_id = $vip['id'];
                $user->vip_end_time = $val['C'].' 23:59:59';
                $user->save();
            }
            $this->getMessageCode('adminMessage.success.insert',2);
            Db::commit();
            return $return;
        }catch (\Exception $exception){
            Db::rollback();
            $this->ERROR=$exception->getMessage();
            return false;
        }
    }
}